// 配置体检相关接口
const express = require('express')
const router = express.Router()
const Response = require('../utils/Response.js')

// 引入mysql连接池
const pool = require('../utils/db.js')
const { error } = require('../utils/Response.js')

// 新增体检
router.post('/ChekUp/add',(req,res) =>{
  let {hospital_id,c_id,p_name,c_price,c_time,package_name} = req.body
  let sql = 'insert into check_up (hospital_id,c_id,p_name,c_price,c_time,package_name) values (?,?,?,?,?,?)'
  pool.query(sql,[hospital_id,c_id,p_name,c_price,c_time,package_name],(err,result) =>{
    if(err){
      res.send(Response.error(500,error))
      throw err
    }
    // 将结果返回给客户端
    res.send(Response.ok(result))
  })
})

// 取消体检
router.delete('/ChekUp/delete',(req,res) =>{
  let {c_id} = req.body
  let sql = 'delete from check_up where c_id=?'
  pool.query(sql,[c_id],(err,result) =>{
    if(err){
      res.send(Response.error(500,error))
      throw err
    }
    // 将结果返回给客户端
    res.send(Response.ok(result))
  })
})

// 修改个人体检
router.post('/ChekUp/update',(req,res) =>{
  let {hospital_id,p_name,c_price,c_time,package_name,c_id} = req.body
  let sql = 'update check_up set hospital_id=?,p_name=?,c_price=?,c_time=?,package_name=? where c_id=?'
  pool.query(sql,[hospital_id,p_name,c_price,c_time,package_name,c_id],(err,result) =>{
    if(err){
      res.send(Response.error(500,error))
      throw err
    }
    // 将结果返回给客户端
    res.send(Response.ok(result))
  })
})

// 查询个人体检
router.get('/ChekUpByPname',(req,res) =>{
  let {p_name} = req.query
  let sql = 'select * from check_up where p_name=?'
  pool.query(sql,[p_name],(err,result) =>{
    if(err){
      res.send(Response.error(500,error))
      throw err
    }
    // 将结果返回给客户端
    res.send(Response.ok(result))
  })
})

// 根据医院id查询体检
router.get('/ChekUpByhospital_id',(req,res) =>{
  let {hospital_id} = req.query
  let sql = 'select * from check_up where hospital_id=?'
  pool.query(sql,[hospital_id],(err,result) =>{
    if(err){
      res.send(Response.error(500,error))
      throw err
    }
    // 将结果返回给客户端
    res.send(Response.ok(result))
  })
})

module.exports = router